package com.tina.util;

import com.tina.entity.Flowers;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.math.BigDecimal;
import java.net.InetAddress;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author tina
 * @Time 2020/10/30 上午10:01
 * @Version 1.0
 * @Content
 **/
@Component
public class HtmlParseUtil {

    /**
     * 获取jd数据
     *
     * @param key
     * @return
     * @throws IOException
     */
    public List<Flowers> parseJd(String key) throws IOException {
        String url = "https://search.jd.com/Search?keyword=" + key;
        // 解析网页
        Document parse = Jsoup.parse(new URL(url), 3000);
        // 所有的html使用的方法，这里都可以使用
        Element elements = parse.getElementById("J_goodsList");
        List<Flowers> list = new ArrayList<>();

        /**
         * 遍历数据
         */
        Elements li = elements.getElementsByTag("li");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:ss");
        // 遍历获取数据

        try {
            for (Element el : li) {
                Flowers flowers = new Flowers();
                String src = el.getElementsByTag("img").eq(0).attr("data-lazy-img");
                // 会员价格
                String memberPrice = el.getElementsByClass("price-plus-1").eq(0).text();
                // 原价
                String text = el.select(".p-price > strong > i").text();
                String name = el.getElementsByClass("p-name").eq(0).text();
                flowers.setTitle(name);
                flowers.setFlowersImg(src);
                flowers.setCover(src);
                double parseDouble = Double.parseDouble(text);
                if (!memberPrice.equals("") && memberPrice != null) {
                    flowers.setDiscount(Integer.valueOf(String.valueOf(Double.parseDouble(memberPrice.substring(1)) / parseDouble).substring(2, 4)));
                    flowers.setSellPrice(BigDecimal.valueOf(Double.parseDouble(memberPrice.substring(1))));
                } else {
                    flowers.setDiscount(90);
                    flowers.setSellPrice(BigDecimal.valueOf(BigDecimal.valueOf(parseDouble - (parseDouble * 0.1)).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue()));
                }
                flowers.setStatus(0);
                flowers.setOriginPrice(BigDecimal.valueOf(parseDouble));
                flowers.setAddTime(simpleDateFormat.format(new Date()));
                list.add(flowers);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    public static void main(String[] args) throws IOException {
//        new HtmlParseUtil().parseJd("鲜花").forEach(System.out::println);

        InetAddress inetAddress = InetAddress.getByName("pigs.gold");
        System.out.println(inetAddress);
        System.out.println(inetAddress.getCanonicalHostName());
        System.out.println(inetAddress.getHostAddress());
        System.out.println(inetAddress.getHostName());
    }
}
